import { storage, tokenName } from '@/config/index'
import Cookies from 'js-cookie' 

type StorageType = 'localStorage' | 'sessionStorage' | 'cookie'

/**
 * 获取accessToken
 */
export function getAccessToken(): string | null | undefined {
  if (storage === 'localStorage') {
    return localStorage.getItem(tokenName)
  } else if (storage === 'sessionStorage') {
    return sessionStorage.getItem(tokenName)
  } else if (storage === 'cookie') {
    return Cookies.get(tokenName)
  } else {
    return localStorage.getItem(tokenName)
  }
}

/**
 * 存储accessToken
 */
export function setAccessToken(accessToken: string): void {
  if (storage === 'localStorage') {
    localStorage.setItem(tokenName, accessToken)
  } else if (storage === 'sessionStorage') {
    sessionStorage.setItem(tokenName, accessToken)
  } else if (storage === 'cookie') {
    Cookies.set(tokenName, accessToken)
  } else {
    localStorage.setItem(tokenName, accessToken)
  }
}

/**
 * 移除accessToken
 */
export function removeAccessToken(): void {
  if (storage === 'localStorage') {
    localStorage.removeItem(tokenName)
  } else if (storage === 'sessionStorage') {
    sessionStorage.removeItem(tokenName)
  } else if (storage === 'cookie') {
    Cookies.remove(tokenName)
  } else {
    localStorage.removeItem(tokenName)
  }
} 